﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using VT.Framework.Designer.Flow;

namespace VIA.VTPlatformService
{
    public class VTFlowBlockWrapper : IDisposable
    {
        public VTFlowBlock VTFlowBlock { get; private set; }
        public DateTime? StartWorkingTime { get; private set; }
        public AutoResetEvent IsEnableEvent { get; private set; } = new AutoResetEvent(true);
        public bool IsBusy { get; private set; }
        public int ProjectId { get; set; }

        public VTFlowBlockWrapper(VTFlowBlock vTFlowBlock, int projectId)
        {
            VTFlowBlock = vTFlowBlock;
            ProjectId = projectId;
        }

        public void Work()
        {
            IsBusy = true;
            IsEnableEvent.Reset();
            StartWorkingTime = DateTime.Now;
        }

        public void UnWork()
        {
            IsBusy = false;
            IsEnableEvent.Set();
            StartWorkingTime = null;
        }

        public TimeSpan GetWorkingTime()
        {
            if (!IsBusy || StartWorkingTime == null) return TimeSpan.Zero;
            try
            {
                return DateTime.Now - (DateTime)StartWorkingTime;
            }
            catch (Exception)
            {
                return TimeSpan.Zero;
            }
        }

        public void Dispose()
        {
            UnWork();
        }
    }
}
